#include <iostream>
#include <memory>
#include <vector>
#include <memory>
#include <thread>

// 类型定义
using Status = int;
using ElemType = int;

// 常量定义
constexpr int TRUE = 1;
constexpr int FALSE = 0;
constexpr int OK = 1;
constexpr int ERROR = 0;
constexpr int OVERFLOW = -1;
constexpr int LONGTH = 5;


// 顺序栈的类型
class SqStack {
public:
    explicit SqStack(int initialSize = 5, int increment = 5)
        : elem_(std::make_unique<std::vector<ElemType>>(initialSize)),
          top_(0),
          size_(initialSize),
          increment_(increment) {}

    ~SqStack() = default;

    // 初始化顺序栈
    Status InitStack(int size, int inc) {
        if (size != this->size_ || inc != this->increment_) {
            // 仅在需要时调整大小
            elem_ = std::make_unique<std::vector<ElemType>>(size);
            this->size_ = size;
            this->increment_ = inc;
        }
        return OK;
    }

    // 销毁顺序栈
    static Status DestroyStack() {
        // 使用智能指针自动管理内存
        return OK;
    }

    // 判断栈是否为空
    bool IsEmpty() const {
        return top_ == 0;
    }

    // 清空栈
    void ClearStack() {
        top_ = 0;
    }

    // 元素入栈
    Status Push(ElemType e) {
        if (top_ >= size_) {
            elem_->resize(size_ + increment_);
            size_ += increment_;
        }
        elem_->at(top_++) = e;
        return OK;
    }

    // 取栈顶元素
    Status GetTop(ElemType &e) const {
        if (IsEmpty()) return ERROR;
        e = elem_->at(top_ - 1);
        return OK;
    }

    // 栈顶元素出栈
    Status Pop(ElemType &e) {
        if (IsEmpty()) return ERROR;
        e = elem_->at(--top_);
        return OK;
    }

    int GetSize() const {return size_;}
    int GetIncrement() const {return increment_;}

private:
    std::unique_ptr<std::vector<ElemType>> elem_;  // 存储栈元素
    int top_;                                      // 栈顶指针
    int size_;                                     // 栈的大小
    int increment_;                                // 每次增长的步长
};


int main() {
    SqStack S(LONGTH, LONGTH);
    ElemType e, eArray[LONGTH] = {1, 2, 3, 4, 5};

    std::cout << "---【顺序栈】---\n";
    std::cout << "栈S的size为：" << S.GetSize() << "\n栈S的increment为：" << S.GetIncrement() << "\n";
    std::cout << "待测试元素为：\n";

    for (const auto& value : eArray) {
        std::cout << value << '\t';
    }
    std::cout << '\n';

    // 入栈
    for (const auto& value : eArray) {
        if (S.Push(value) != OK) {
            std::cout << value << "入栈失败\n";
            return 1;
        }
    }
    std::cout << "已入栈\n";
     std::this_thread::sleep_for(std::chrono::seconds(1000));
    // 判断非空
    if (S.IsEmpty()) std::cout << "S栈为空\n";
    else std::cout << "S栈非空\n";

    // 取栈顶元素
    std::cout << "栈S的栈顶元素为：\n";
    S.GetTop(e);
    std::cout << e << '\n';

    // 栈元素出栈
    std::cout << "栈S元素出栈为：\n";
    while (!S.IsEmpty()) {
        S.Pop(e);
        std::cout << e << '\t';
    }
    std::cout << '\n';

    // 清空栈
    S.ClearStack();
    std::cout << "已清空栈S\n";

    return 0;
}